package com.smile.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.spring.SaBeanInject;
import cn.hutool.core.bean.BeanUtil;
import com.smile.commons.page.R;
import com.smile.model.dto.request.DeleteFileRequestDTO;
import com.smile.model.response.response.PutFileResponseDTO;
import com.smile.model.request.DeleteFileRequest;
import com.smile.model.response.PutFileResponse;
import com.smile.service.OssService;
import feign.RequestLine;
import org.hibernate.validator.internal.constraintvalidators.bv.size.SizeValidatorForArraysOfInt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/oss")
public class OssController {

    @Autowired
    private OssService ossService;

    @PostMapping(value = "/putFile", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public R<PutFileResponse> putFile(@RequestParam String path, @RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID().toString().replace("-", "") + ".png";
        PutFileResponseDTO b = ossService.putFile(path, fileName, file);
        PutFileResponse response = new PutFileResponse();
        BeanUtil.copyProperties(b, response);
        return R.ok(response);
    }

    @PostMapping(value = "/putPcmFile", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public R<PutFileResponse> putPcmFile(@RequestParam String path, @RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID().toString().replace("-", "") + ".pcm";
        PutFileResponseDTO b = ossService.putFile(path, fileName, file);
        PutFileResponse response = new PutFileResponse();
        BeanUtil.copyProperties(b, response);
        return R.ok(response);
    }


    @SaIgnore
//    @PostMapping(value = "/putkmlFile", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
    @PostMapping(value = "/putkmlFile", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
//    public R<PutFileResponse> putkmlFile(@RequestParam String fileName, @RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
//    @RequestLine("POST /putkmlFile")
    public R<PutFileResponse> putkmlFile(@RequestParam String fileName,@RequestPart MultipartFile fileStream) throws IOException {
//        String fileName = UUID.randomUUID().toString().replace("-", "") + ".pcm";
        PutFileResponseDTO b = ossService.putFile("/", "kml" + File.separator + fileName, fileStream);
        PutFileResponse response = new PutFileResponse();
        BeanUtil.copyProperties(b, response);
        return R.ok(response);
    }

    @PostMapping(value = "/deleteFile", consumes = MediaType.APPLICATION_JSON_VALUE)
    public R deleteFile(@RequestBody DeleteFileRequest request) throws IOException {
        DeleteFileRequestDTO deleteFileRequestDTO = new DeleteFileRequestDTO();
        BeanUtil.copyProperties(request, deleteFileRequestDTO);
        Boolean result = ossService.deleteFile(deleteFileRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
}
